package com.buba.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.buba.pojo.user.Inpatient;
import com.buba.user.service.InpatientService;
import com.buba.user.service.ThePatientService;
import com.buba.user.vo.InpatientVo;
import com.buba.utils.R;
import com.buba.utils.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 住院人表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-06-09
 */
@RestController
@RequestMapping("/user/inpatient")
@Slf4j
public class InpatientController {

    @Autowired
    private InpatientService inpatientService;

    @Autowired
    private ThePatientService thePatientService;

    //绑定住院人
    @GetMapping("/bindInpatient/{id}")
    public R bindInpatient(@PathVariable Integer id){
        log.info("id::::"+id);
        boolean save = inpatientService.bindInpatient(id);
        Integer userId = thePatientService.getById(id).getUserId();
        log.info("userId:::"+userId);
        if (save){
            return R.ok().data("message" , "添加成功").data("id" , userId);
        }else{
            return R.error().data("message" , "添加成功");
        }
    }

    //查询住院人信息
    @GetMapping("/hospitalById/{id}")
    public R hospitalById(@PathVariable Integer id){
        log.info("userId:::"+id);
        InpatientVo hospital =inpatientService.hospitalById(id);
        return R.ok().data("hospital" , hospital);

    }

    //根据用户id查询住院人
    @GetMapping("/inpatientUserId/{id}")
    public R inpatientUserId(@PathVariable Integer id){
        log.info("userId:::"+id);
        List<InpatientVo> list =inpatientService.inpatientUserId(id);
        return R.ok().data("inpatients" , list);

    }

    //添加住院人
    @PostMapping("/addinpatient")
    public R addinpatient(@RequestBody Inpatient inpatient){

        Integer day = inpatient.getUserId();
        System.out.println("day===="+day);
        Date d = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format1 = format.format(d);
        //System.out.println("现在的日期是：" + currdate);
        Calendar ca = Calendar.getInstance();
        ca.setTime(d);
        ca.add(Calendar.DATE, day);// num为增加的天数，可以改变的
        d = ca.getTime();

        inpatient.setUserId(null);

        inpatient.setHospitalizedTime(new Date());
        inpatient.setLeavehospitalTime(d);
        inpatient.setCreationTime(new Date());
        inpatient.setModificationTime(new Date());
        inpatient.setAdmissionNumber(RandomUtil.getSixBitRandom());

        inpatient.setUserId(1);


        boolean save =inpatientService.save(inpatient);
        int i=inpatient.getId();
        System.out.println("id======"+i);
        if(save==true){
            return R.ok().data("ok","添加成功").data("i",i);
        }else {
            return R.ok().data("error","添加失败");
        }
    }


}

